// pages/user/userIndex/userIndex.js
const app = getApp();
import Api from '../../../../utils/http';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    identityStatus: 1, //0为管理员身份，1为部门管理员
    usertype: 2, //当identityStatus 为1时，userStatus为1是部门管理员，为2是用户
    tabBarIndex: 3, //当前tabBar索引显示
    unitStatus: 2, //1 已申请单位，2未申请单位
    data: null,
    dataTotal: null,
    isHide: false,
    userinfo: null,
    webSiteUrl: app.globalData.webSiteUrl,
    type: null,//状态值
  },

  //跳转个人资料
  toUserInfo: function () {
    var userStatus = this.data.userStatus;
    wx.navigateTo({
      url: '/pages/user/user/userInfo/userInfo?userStatus=' + userStatus,
    })
  },
  //跳转个人设置
  toUserSet: function () {
    wx.navigateTo({
      url: '/pages/user/user/userSetting/userSetting',
    })
  },
  //切换角色——用户
  toSwitch:function(e){
    this.getRoleSwitch(e);
  },
  //跳转联系客服
  contactService: function () {
    wx.navigateTo({
      url: '/pages/user/user/contactService/contactService',
    })
  },
  //跳转我的单位
  toMyUnit: function () {
    wx.navigateTo({
      url: '/pages/user/user/myUnit/myUnit',
    })
  },
  //跳转我的关注
  myInterest: function () {
    wx.navigateTo({
      url: '/pages/user/user/userInterest/userInterest',
    })
  },
  //跳转申请单位
  toApplyUnit: function () {
    var that = this;
    that.getuserput();
  },
  //跳转我的钱包
  toMyWallet: function () {
    wx.navigateTo({
      url: '/pages/user/user/userWallet/userWallet',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        }
      }
    })
  },
  //跳转我的回收袋
  toMyRecycling: function () {
    wx.navigateTo({
      url: '/pages/user/user/userRecycling/userRecycling',
    })
  },
  //跳转我的投放
  toMyPutIn: function () {
    wx.navigateTo({
      url: '/pages/user/user/userPutIn/userPutIn',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // that.getBalanceList();
    //that.getusertext();
    getApp().watch(that.watchBack)
    setTimeout(function () {
      app.globalData.name = 'onload'
    }, 3000)
  },

  watchBack: function (name) {
    console.log(22222);
    console.log('this.name==' + name)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // setTimeout(function(){
    //   app.globalData.name = 'onshow'
    // },3000) 
    this.getusertext();
  },
  //个人信息详情
  getusertext: function () {
    console.log(this.data)
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    console.log(param)
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.setStorageSync('userid', res.data);
        if (res.data.user_type == 3) {
          that.setData({
            identityStatus: 0,
          })
        } else if (res.data.user_type == 2) {
          that.setData({
            identityStatus: 1,
            userStatus: 1
          })
        } else if (res.data.user_type == 1) {
          var unitStatus = 2;
          if (res.data.verification_manage == 1 || res.data.is_manage == 1) {
            unitStatus = 1;
          } else if (res.data.verification_depart == 1 || res.data.is_department == 1) {
            unitStatus = 1;
          }
          that.setData({
            identityStatus: 1,
            userStatus: 2,
            unitStatus: unitStatus,
          })
        }
        that.setData({
          userInfo: res.data,
        })
      }
    })
  },
  //单位判断
  getuserput: function () {
    console.log(this.data)
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    console.log(param)
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        if (res.data.apply_manage != null ) {
          if (res.data.apply_manage.status == 0) {
            wx.showToast({
              title: '已提交申请，请等待',
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.apply_manage.status == 2) {
            wx.navigateTo({
              url: '/pages/user/user/applyResult/applyResult',
            })
          } else if (res.data.verification != 1) {
            wx.showToast({
              title: '该用户未认证',
              icon: 'none',
              duration: 2000
            })
          } else {
            wx.navigateTo({
              url: '/pages/user/user/applyUnit/applyUnit',
            })
          }
        }else{
          wx.navigateTo({
            url: '/pages/user/user/applyUnit/applyUnit',
          })
        }
      }
    })
  },
  
//切换用户
getRoleSwitch: function (e) {
  var that = this;
  let uid = wx.getStorageSync('userid');
  console.log(e)
  var param = {
    uid: uid.id,
    user_type: uid.user_type,
    type: e.currentTarget.dataset.type,
  }
  console.log(param)
  Api.ajax('/app/Zindexmanage/roleSwitch', param).then(res => {
    console.log(res)
    if (res.code == 200) {
      wx.setStorageSync('userid', res.data);
      setTimeout(function(){
        wx.reLaunch({
          url: '/pages/index/index',
        })
      },1000)
      wx.showToast({
        title: '切换中，请等待',
      })
    }else{
      wx.showToast({
        title: '切换失败，请刷新重试',
      })
    }
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})